<?php
require APPPATH.'libraries/ApiController.php';
class profile extends ApiController {
	public function __construct()
	{
		parent::__construct();
	}
	
	//根据用户获取功能模块
	public function index(){
		$token=$this->verifyToken();
		if($token){
			//获取用户信息
			$this->db->join("admin_role",'admin_role.roleid=admin.roleid');
			$user=$this->db->where("uid",$token["uid"])->get("admin")->row_array();
			unset($user["password"]);
			if($user["avatar"]!=""){
				$user["avatar"]=$this->config->item("domain").$user["avatar"];
			}
			
			//检测此账户名下是否有店铺
			$shop=$this->db->where("uid",$token["uid"])->get("shop")->row_array();
			if($shop){
				$shop["logo"]=$this->config->item("domain").$shop["logo"];

				$mids=array(22,14,9,11,12,13,15);
				$this->db->select("pageid,title,url,order,concat('".$this->config->item("static")."',icon) as icon,group,createtime");
				$modules=$this->db->where_in("pageid",$mids)->get("page")->result_array();
				
				$this->WRITE("code",1);
				$this->WRITE("user",$user);
				$this->WRITE("shop",$shop);
				$this->WRITE("shopid",$shop["shopid"]);
				$this->WRITE("modules",$modules);
				$this->RESPONSE();
			}else{
				$this->WRITE("code",1);
				$this->WRITE("userinfo",$user);
				$this->WRITE("shop",null);
				$this->WRITE("shopid",-1);
				$this->WRITE("modules",[]);
				$this->RESPONSE();
			}
		}
	}
	
	
	public function basicinfo(){
		$token=$this->verifyToken();
		if($token){
			$this->db->select("uid,mobile,nickname,avatar,gender,password,memo,address,email,birth,idcard,admin.shopid,admin_role.roleid,admin_role.roletitle");
			$this->db->join("admin_role","admin_role.roleid=admin.roleid","left");
			$user=$this->db->where("uid",$token["uid"])->get("admin")->row_array();
			unset($user["password"]);
			if($user["avatar"]!=""){
				$user["avatar"]=$this->config->item("domain").$user["avatar"];
			}
			
			if($user){
				$this->WRITE("msg","查询成功!");
				$this->WRITE("data",$user);
				$this->WRITE("code",1);
				$this->RESPONSE();
			}else{
				$this->WRITE("msg","查询失败!");
				$this->WRITE("code",0);
				$this->RESPONSE();
			}
		}
		
	}
	
	//更新个人信息
	public function update_basicinfo(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["uid",true,"UID"],
			["nickname",true,"会员姓名"],
			["mobile",true,"会员手机号"],
			["gender",true,"会员性别"],
			["avatar",false,"头像",""],
			["birth",false,"会员生日",""],
			["idcard",false,"身份证号",""],
			["email",false,"电子邮件",""],
			["address",false,"会员地址",""],
			["memo",false,"备注信息",""]],'post');
			
			//查询是否存在此手机号
			$user=$this->db->where("mobile",$data["mobile"])->get("admin")->row_array();
			if($user && ($user["uid"]!=$data["uid"])){
				$this->WRITE("code",0);
				$this->WRITE("msg","此手机号已被其他人使用！");
				$this->RESPONSE();
				exit();
			}
			//如果有上传的文件
			if($_FILES){
				$oldavatar=$user["avatar"];
				//删除头像
				if($oldavatar!=""){
					if(file_exists($this->config->item("upload").$oldavatar)){
						unlink($this->config->item("upload").$oldavatar);
					}
				}
				
				//上传头像
				if (!file_exists($this->config->item("upload")."/uploads/avatar/")) {
				    mkdir($this->config->item("upload")."/uploads/avatar/", 0777, true);
				}
				//保存上传头像
				foreach ($_FILES as $key => $file) {
					if (strpos($file["type"], 'image') !== false  ) {
						$ext = pathinfo($file["name"],PATHINFO_EXTENSION);
						$filename = 'admin_avatar_'.uniqid().'.'.$ext;
						$path='/uploads/avatar/'.$filename;
						if (move_uploaded_file($file["tmp_name"],$this->config->item("upload").$path)) {
							//上传成功
							$data["avatar"]=$path;
						}
					}
				}
			}else{
				unset($data["avatar"]);
			}
			//更新数据及头像
			$this->db->where("uid",$data["uid"])->update("admin",$data);
			
			$this->db->select("uid,mobile,nickname,avatar,gender,memo,address,email,birth,idcard,admin.shopid,admin_role.roleid,admin_role.roletitle");
			$this->db->join("admin_role","admin_role.roleid=admin.roleid","left");
			$user=$this->db->where("uid",$token["uid"])->get("admin")->row_array();

			$this->WRITE("code",1);
			$user["avatar"]=$user["avatar"]==""? "":$this->config->item("domain").$user["avatar"];
			$this->WRITE("userinfo",$user);
			$this->WRITE("msg","更新成功!");
			$this->RESPONSE();
		}
	}
	
	public function update_password(){
		$token=$this->verifyToken();
		if($token){
			$data=$this->getKeys([
			["password",true,"密码"],
			["password1",true,"确认密码"]],'post');
			
			
			$res=$this->db->where("uid",$token["uid"])->update("admin",array("password"=>$data["password"]));
			
			$this->WRITE("code",1);
			$this->WRITE("msg","更新成功!");
			$this->RESPONSE();
			
		}
	}
}
	